/**
 *
 */
package com.et114.modules.org.dao;

import java.util.List;

import com.et114.components.acegi.Constants;
import com.et114.core.common.AppContext;
import com.et114.core.dao.impl.BaseDAOImpl;
import com.et114.core.exception.CheckException;
import com.et114.modules.org.vo.EmpTitleRelVo;
import com.et114.modules.org.vo.GroupTitleRelVO;
import com.et114.modules.org.vo.TitleMsgVo;
import com.et114.modules.org.vo.TitleRoleRelVo;

//import com.et114.modules.org.vo.UserEmplyVo;

/*
 * @author guanhw
 */
public class TitleDAO extends BaseDAOImpl {
	/**
	 * @return
	 * @throws CheckException
	 */
	public List getTitles ( ) throws CheckException {
		return getTitles ( new TitleMsgVo ( ) );
	}
	
	/**
	 * @param vo
	 * @return
	 */
	public List getTitles ( TitleMsgVo vo ) throws CheckException {
		return queryList ( vo , "getTitle" );
	}
	
	public TitleMsgVo getTitle ( TitleMsgVo vo ) throws CheckException {
		return ( TitleMsgVo ) load ( vo , "getTitle" );
	}
	
	public List loadTitlesByIds( TitleMsgVo vo ) throws CheckException { 
		return queryList ( vo , "loadTitlesByIds"  ) ;
	}
	
	/**
	 * 数据库分页
	 * @param vo
	 * @return
	 * @throws CheckException
	 */
	public List getRolesByTitle ( TitleMsgVo vo ) throws CheckException {
		return queryList ( vo , "getRolesByTitle" );
	}
	
	public Long countgetRolesByTitle( TitleMsgVo vo ) throws CheckException { 
		return getCount( vo , "countgetRolesByTitle" ) ;
	}
	
	public List getRolesByTitleNPage ( TitleMsgVo vo ) throws CheckException {
		return queryList ( vo , "getRolesByTitleNPage" );
	}
	
	
	public List getRolesByTitle ( Long titleId ) throws CheckException {
		TitleMsgVo vo = new TitleMsgVo() ; vo.setTitleId ( titleId ) ;
		return queryList ( vo , "getRolesByTitleNPage" );
	}	
	
	public List getUsersByTitle ( TitleMsgVo vo ) throws CheckException {
		return queryList ( vo , "getUsersByTitle" );
	}
	
	public int countUsersByTitle( TitleMsgVo vo ) throws CheckException { 
		return getCount( vo , "countUsersByTitle" ).intValue ( ) ;
	}
	
	
	public List getGroupsByTitle ( TitleMsgVo vo ) throws CheckException {
		return queryList ( vo , "getGroupsByTitle" );
	}
	
	public List getFunctionsByTitle ( Long titleId ) throws CheckException {
		return queryList ( titleId , "getFunctionsByTitle" );
	}
	
	public List getFuncComponentsByTitle( Long titleId ) throws CheckException { 
		return queryList ( titleId , "getFuncComponentsByTitle" );
	}
	
	public Long createTitle ( TitleMsgVo titleMsgVo ) throws CheckException {
		Long result = ( Long ) insert ( titleMsgVo , "createTitle" );
		if ( AppContext.isHasLog (  ) ) {		
			titleMsgVo.setOperType ( Constants.LOG_OPER_NEW) ;
			addLogTitle( titleMsgVo ) ; 
		} 
		return result;
	}
	
	public Long createGroupTitleRel ( GroupTitleRelVO groupTitleRelVO )	throws CheckException {
		Long result = ( Long ) insert ( groupTitleRelVO , "createGroupTitleRel" );
		if ( AppContext.isHasLog (  ) ) {		
			groupTitleRelVO.setOperType ( Constants.LOG_OPER_NEW) ;
			addLogGrpTitleRel( groupTitleRelVO ) ; 
		}
		return result;
	}
	
	public Long createTitleRoleRel ( TitleRoleRelVo titleRoleRelVO ) throws CheckException {
		Long result = ( Long ) insert ( titleRoleRelVO , "createTitleRoleRel" );
		if ( AppContext.isHasLog (  ) ) {		
			titleRoleRelVO.setOperType ( Constants.LOG_OPER_NEW ) ;
			titleRoleRelVO.setOperUser ( titleRoleRelVO.getCrtUser ( ) ) ;
			addLogTitleRoleRel( titleRoleRelVO ) ; 
		}
		return result;
	}
	
	public Long createEmpTitleRel ( EmpTitleRelVo empTitleRelVo )
			throws CheckException {
		Long result = ( Long ) insert ( empTitleRelVo , "createEmpTitleRel" );
		if ( AppContext.isHasLog (  ) ) {		
			empTitleRelVo.setOperType ( Constants.LOG_OPER_NEW) ;
			addLogEmpTitleRel( empTitleRelVo ) ; 
		}
		return result;
	}
	
	public int removeEmpTitleRel ( EmpTitleRelVo empTitleRelVo )
			throws CheckException {
		if ( AppContext.isHasLog (  ) ) {		
			empTitleRelVo.setOperType ( Constants.LOG_OPER_REMOVE) ;
			addLogEmpTitleRel( empTitleRelVo ) ; 
		}
		return delete ( empTitleRelVo , "removeEmpTitleRel" );
	}
	
	public List queryTitlesOfEmp ( Long empId ) throws CheckException {
		return queryList ( empId , "queryTitlesOfEmp" );
	}
	
	public List queryTitlesOfGroupPage( TitleMsgVo titleMsgVo  ) throws CheckException {  
		return queryList( titleMsgVo , "queryTitlesOfGroupPage" ) ;
	}
	
	public Long countqueryTitlesOfGroup( Long groupId ) throws CheckException {   
		return getCount( groupId , "countqueryTitlesOfGroup" ) ;
	}
	
	public TitleMsgVo loadPrimaryTitleOfUser ( Long userId )throws CheckException {
		return ( TitleMsgVo ) load ( userId , "loadPrimaryTitleOfUser" );
	}
	
	public int deleteGroupTitleRel ( GroupTitleRelVO groupTitleRelVO )	throws CheckException {
		Long operuser = groupTitleRelVO.getOperUser ( );
		if ( AppContext.isHasLog (  ) ) {		
			groupTitleRelVO = this.getGroupsByTitleID ( groupTitleRelVO.getTitleId ( ) );
			groupTitleRelVO.setOperType ( Constants.LOG_OPER_REMOVE) ;
			groupTitleRelVO.setOperUser ( operuser );
			addLogGrpTitleRel( groupTitleRelVO ) ; 
		}
		return delete ( groupTitleRelVO , "deleteGroupTitleRel" );
	}
	
	public int deleteTitleRoleRel( TitleRoleRelVo titleRoleRel ) throws CheckException { 
		if ( AppContext.isHasLog (  ) ) {		
			titleRoleRel.setOperType ( Constants.LOG_OPER_REMOVE) ;
			addLogTitleRoleRel( titleRoleRel ) ; 
		}
		return delete ( titleRoleRel , "deleteTitleRoleRel" );
	}
	
	public int deleteTitle ( Long titleId , Long operuser ) throws CheckException {
		if ( AppContext.isHasLog (  ) ) {		
			TitleMsgVo titleMsgVo = this.loadTitle ( titleId );
			titleMsgVo.setOperType ( Constants.LOG_OPER_REMOVE) ;
			titleMsgVo.setOperUser ( operuser );
			addLogTitle( titleMsgVo ) ; 
		}
		return delete ( titleId , "deleteTitle" );
	}
	
	
	public void deleteTitleRoleRelByTitleId( Long titleId , Long operuser ) throws CheckException {
		//delete ( titleId , "deleteTitleRoleRelByTitleId" );
		TitleRoleRelVo titleRoleRel = new TitleRoleRelVo( ) ;
		titleRoleRel.setTitleId ( titleId ) ; 
		titleRoleRel.setOperUser ( operuser ) ;
		titleRoleRel.setOperType ( Constants.LOG_OPER_REMOVE ) ; 
		deleteTitleRoleRel( titleRoleRel ) ;
	}
	
	public void deleteTitleRoleRelByRoleId( Long roleId , Long operuser ) throws CheckException {
		TitleRoleRelVo titleRoleRel = new TitleRoleRelVo( ) ;
		titleRoleRel.setRoleId ( roleId ) ; 
		titleRoleRel.setOperUser ( operuser ) ;
		titleRoleRel.setOperType ( Constants.LOG_OPER_REMOVE ) ; 
		deleteTitleRoleRel( titleRoleRel ) ;
	}
	
	public TitleMsgVo loadTitle ( Long titleId ) throws CheckException {
		return ( TitleMsgVo ) load ( titleId , "loadTitle" );
	}
	
	public void updateTitle ( TitleMsgVo titleMsgVo ) throws CheckException {
		update ( titleMsgVo , "updateTitle" );
		if ( AppContext.isHasLog (  ) ) {		
			titleMsgVo.setOperType ( Constants.LOG_OPER_UPDATE) ;
			addLogTitle( titleMsgVo ) ; 
		}
	}
	
	public List queryTitlesOfUser ( Long userId ) throws CheckException {
		return queryList ( userId , "queryTitlesOfUser" );
	}
	
	public Long countTitles ( TitleMsgVo titleMsgVo ) throws CheckException {
		return getCount ( titleMsgVo , "countTitles" );
	}
	
	public void addLogTitle( TitleMsgVo titleMsgVo )  throws CheckException { 
		insert ( titleMsgVo , "addLogTitle" ) ;
	}
	
	public void addLogGrpTitleRel( GroupTitleRelVO groupTitleRelVo )  throws CheckException { 
		insert ( groupTitleRelVo , "addLogGrpTitleRel" ) ;
	}
	
	public void addLogTitleRoleRel( TitleRoleRelVo titleRoleRelVO )  throws CheckException { 
		insert ( titleRoleRelVO , "addLogTitleRoleRel" ) ;
	}
	
	public void addLogEmpTitleRel( EmpTitleRelVo empTitleRelVo )  throws CheckException { 
		insert ( empTitleRelVo , "addLogEmpTitleRel" ) ;
	}
	
	public GroupTitleRelVO getGroupsByTitleID(Long titleId) throws CheckException {
        return (GroupTitleRelVO) load(titleId, "getGroupsByTitleID");
    }
	
}
